package chabernac.android.wordbattle;

import java.util.List;

import junit.framework.TestCase;
import chabernac.android.wordbattle.db.CommentWordFilter;
import chabernac.android.wordbattle.db.CompositeWordFilter;
import chabernac.android.wordbattle.db.InMemoryTreeDB;
import chabernac.android.wordbattle.db.InvalidWordFilter;
import chabernac.android.wordbattle.db.LengthWordFilter;
import chabernac.android.wordbattle.db.URLWordLoader;
import chabernac.android.wordbattle.db.iStringDatabase;
import chabernac.android.wordbattle.db.iWordFilter;
import chabernac.android.wordbattle.db.iWordLoader;
import chabernac.android.wordbattle.model.Letter;
import chabernac.android.wordbattle.model.WordRaster;
import chabernac.android.wordbattle.model.WordRasterFactory;

public class SelectedLetterHelperTest extends TestCase {
  private iStringDatabase myStringDatabase = new InMemoryTreeDB();
  private iWordLoader myWordLoader = new URLWordLoader( "http://wordbattle.googlecode.com/files" );
  private iWordFilter myWordFilter = new CompositeWordFilter().addFilter( new CommentWordFilter() ).addFilter( new LengthWordFilter( 3, 4 ) ).addFilter( new InvalidWordFilter() );
  private WordRasterFactory myWordRasterFactory = new WordRasterFactory( myStringDatabase );

  
  public void testSelectedLetterHelper(){
    WordRaster theRaster = myWordRasterFactory.createTestRaster("nl", 4, 4);
    
    SelectedLetterHelper theHelper = new SelectedLetterHelper(theRaster);
    
    theHelper.down(0, 0);
    theHelper.down(2, 0);
    theHelper.down(2, 3);
    
    //3 letters must be selected
    
    List<Letter> theSelectedLetters = theRaster.getSelectedLetters();
    
    System.out.println("Selected letters" + theSelectedLetters);
    assertEquals(6, theSelectedLetters.size());
    
    assertEquals(theRaster.getLetter(0, 0), theSelectedLetters.get(0));
    assertEquals(theRaster.getLetter(1, 0), theSelectedLetters.get(1));
    assertEquals(theRaster.getLetter(2, 0), theSelectedLetters.get(2));
    assertEquals(theRaster.getLetter(2, 1), theSelectedLetters.get(3));
    assertEquals(theRaster.getLetter(2, 2), theSelectedLetters.get(4));
    assertEquals(theRaster.getLetter(2, 3), theSelectedLetters.get(5));
  }
  
  public void testSelectedLetterHelperRightToLeft(){
    WordRaster theRaster = myWordRasterFactory.createTestRaster("nl", 4, 4);
    
    SelectedLetterHelper theHelper = new SelectedLetterHelper(theRaster);
    
    theHelper.down(0, 3);
    theHelper.down(0, 0);
    
    //3 letters must be selected
    
    List<Letter> theSelectedLetters = theRaster.getSelectedLetters();
    
    System.out.println("Selected letters" + theSelectedLetters);
    assertEquals(4, theSelectedLetters.size());
    
    assertEquals(theRaster.getLetter(0, 3), theSelectedLetters.get(0));
    assertEquals(theRaster.getLetter(0, 2), theSelectedLetters.get(1));
    assertEquals(theRaster.getLetter(0, 1), theSelectedLetters.get(2));
    assertEquals(theRaster.getLetter(0, 0), theSelectedLetters.get(3));
  }
  
  public void testSelectedLetterBottomToTop(){
    WordRaster theRaster = myWordRasterFactory.createTestRaster("nl", 4, 4);
    
    SelectedLetterHelper theHelper = new SelectedLetterHelper(theRaster);
    
    theHelper.down(3, 0);
    theHelper.down(0, 0);
    
    //3 letters must be selected
    
    List<Letter> theSelectedLetters = theRaster.getSelectedLetters();
    
    System.out.println("Selected letters" + theSelectedLetters);
    assertEquals(4, theSelectedLetters.size());
    
    assertEquals(theRaster.getLetter(3, 0), theSelectedLetters.get(0));
    assertEquals(theRaster.getLetter(2, 0), theSelectedLetters.get(1));
    assertEquals(theRaster.getLetter(1, 0), theSelectedLetters.get(2));
    assertEquals(theRaster.getLetter(0, 0), theSelectedLetters.get(3));
  }

}
